<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>运单快速录入</title>
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
		<link rel="stylesheet" type="text/css" href="../../css/default.css">
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		<script type="text/javascript">
			var editIndex;

			function doAdd() {
				if(editIndex != undefined) {
					$("#grid").datagrid('endEdit', editIndex);
				}
				if(editIndex == undefined) {
					//alert("快速添加电子单...");
					$("#grid").datagrid('insertRow', {
						index: 0,
						row: {}
					});
					$("#grid").datagrid('beginEdit', 0);
					editIndex = 0;
				}
			}

			function doEdit() {
				if(editIndex != undefined) {
					// 正在编辑
					return;
				}
				// 获取到选中行
				var row = $("#grid").datagrid('getSelected');
				var index = $("#grid").datagrid('getRowIndex', row);
				// 编辑选中行
				$("#grid").datagrid('beginEdit', index);
				// 保存到全局变量
				editIndex = index;
			}

			function doSave() {
				$("#grid").datagrid('endEdit', editIndex);
			}

			function doCancel() {
				if(editIndex != undefined) {
					$("#grid").datagrid('cancelEdit', editIndex);
					if($('#grid').datagrid('getRows')[editIndex].id == undefined) {
						$("#grid").datagrid('deleteRow', editIndex);
					}
					editIndex = undefined;
				}
			}

			function doDblClickRow(rowIndex, rowData) {
				alert("双击表格数据...");
				console.info(rowIndex);
				$('#grid').datagrid('beginEdit', rowIndex);
				editIndex = rowIndex;
			}

			//工具栏
			var toolbar = [{
				id: 'button-add',
				text: '新增一行',
				iconCls: 'icon-edit',
				handler: doAdd
			}, {
				id: 'button-edit',
				text: '编辑',
				iconCls: 'icon-edit',
				handler: doEdit
			}, {
				id: 'button-save',
				text: '保存',
				iconCls: 'icon-save',
				handler: doSave
			},{
				id: 'button-cancel',
				text: '取消编辑',
				iconCls: 'icon-cancel',
				handler: doCancel
			}];
			// 定义列
			var columns = [
				[{
					field: 'wayBillNum',
					title: '运单号',
					width: 120,
					align: 'center',
					editor: {
						type: 'validatebox',
						options: {
							required: true
						}
					}
				}, {
					field: 'arriveCity',
					title: '到达地',
					width: 120,
					align: 'center',
					editor: {
						type: 'validatebox',
						options: {
							required: true
						}
					}
				}, {
					field: 'goodsType',
					title: '货物类型',
					width: 120,
					align: 'center',
					editor: {
						type: 'validatebox',
						options: {
							required: true
						}
					}
				}, {
					field: 'num',
					title: '件数',
					width: 120,
					align: 'center',
					editor: {
						type: 'numberbox',
						options: {
							required: true
						}
					}
				}, {
					field: 'weight',
					title: '重量',
					width: 120,
					align: 'center',
					editor: {
						type: 'validatebox',
						options: {
							required: true
						}
					}
				}, {
					field: 'floadreqr',
					title: '配载要求',
					width: 220,
					align: 'center',
					editor: {
						type: 'validatebox',
						options: {
							required: true
						}
					}
				}]
			];

			$(function() {
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({ visibility: "visible" });

				// 运单数据表格
				$('#grid').datagrid({
					iconCls: 'icon-forward',
					fit: true,
					border: true,
					rownumbers: true,
					striped: true,
					pageList: [30, 50, 100],
					pagination: true,
					toolbar: toolbar,
					url: "../../waybill_pageQuery.action",
					idField: 'id',
					columns: columns,
					onDblClickRow: doDblClickRow,
					singleSelect : true , 
					onAfterEdit: function(rowIndex, rowData, changes) {
//						alert("传入一行数据="+JSON.stringify(rowData));
						// 将新添加运单数据 保存到 数据表中 
						$.post("../../waybill_save.action", rowData, function(data) {
							// data数据格式 {success:true, msg:'xxxx'} 
							alert(data.success1+" "+data.msg1+" data="+data);
							if(data.success1) {
								// 保存成功 
								$.messager.show({
									title: '提示窗口',
									msg: data.msg1
								});
							} else {
								// 刷新列表 
								$("#grid").datagrid('reload');
							}
						});
						console.info(rowData);
						editIndex = undefined;
					}
				});
			});
		</script>
	</head>

	<body class="easyui-layout" style="visibility:hidden;">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
	</body>

</html>